package org.example.practiceexamples.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.practiceexamples.dox.Appointment;
import org.example.practiceexamples.dox.Course;
import org.example.practiceexamples.dox.Lab;
import org.example.practiceexamples.dox.User;
import org.example.practiceexamples.exception.Code;
import org.example.practiceexamples.repository.LabRepository;
import org.example.practiceexamples.repository.UserRepository;
import org.example.practiceexamples.service.AdminService;
import org.example.practiceexamples.service.ManageService;
import org.example.practiceexamples.vo.ResultVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/manage/")
public class ManageController {

    private final ManageService manageService;
    private final LabRepository labRepository;
    private final UserRepository userRepository;

//    @PostMapping("saveLab")
//    public ResultVO saveLab(@RequestBody Lab lab){
//        try{
//            manageService.saveLab(lab);
//            return ResultVO.success("成功保存！");
//        }
//        catch(Exception e){
//                log.error("Error saving Lab", e);
//                return ResultVO.error(Code.ERROR, "Failed to save Lab");
//        }
//    }


//    @PostMapping("saveTeacher")
//    public ResultVO saveUser(@RequestBody User user){
//        try{
//            adminService.saveTeacher(user);
//            return ResultVO.success("成功保存！");
//        }
//        catch(Exception e){
//            log.error("Error saving User", e);
//            return ResultVO.error(Code.ERROR, "Failed to save User");
//        }
//    }

//    @DeleteMapping("deleteUser/{account2}")
//    public ResultVO deleteUser(@PathVariable String account2){
//        try{
//            adminService.deleteUser(account2);
//            return ResultVO.success("成功删除！");
//
//        }
//        catch(Exception e){
//            log.error("Error delete User", e);
//            return ResultVO.error(Code.ERROR, "Failed to delete User");
//        }
//    }

    @GetMapping("getLabByName/{lname}")
    public ResultVO getLabByName(@RequestAttribute String uid,@PathVariable String lname) {
        try{
            Lab lab = manageService.findLabById(uid,lname);
            return ResultVO.success(lab,"查询成功!");
        }
        catch (Exception e){
            log.error(e.getMessage());
            return ResultVO.error(Code.ERROR,e.getMessage());
        }
    }

    @GetMapping("getAppointmentsByLname/{lname}")
    public ResultVO getAppointmentsByLname(@RequestAttribute String uid,@PathVariable String lname) {
        try{
            List<Appointment> appointments = manageService.findAppointmentsByLname(uid,lname);
            return ResultVO.success(appointments,"查询成功！");
        }
        catch (Exception e){
            log.error(e.getMessage());
            return ResultVO.error(Code.ERROR,e.getMessage());
        }
    }

    @DeleteMapping("deleteLab/{lname}")
    public ResultVO deleteLab(@RequestAttribute String uid,@PathVariable String lname){
        try{
            manageService.deleteLabByName(uid,lname);
            return ResultVO.success("成功删除！");

        }
        catch(Exception e){
            log.error("Error delete Lab", e);
            return ResultVO.error(Code.ERROR, "Failed to delete Lab");
        }
    }

    @DeleteMapping("deleteAppointment/{aid}")
    public ResultVO deleteAppointment(@PathVariable String aid,@RequestAttribute String uid){
        try{
           manageService.deleteAppointmentById(uid,aid);
            return ResultVO.success("成功删除！");

        }
        catch(Exception e){
            log.error("Error delete User", e);
            return ResultVO.error(Code.ERROR, "Failed to delete User");
        }
    }

    @PostMapping("updateLab")
    public ResultVO updateLabMessage(@RequestAttribute String uid, @RequestBody Lab lab) {
        List<Lab> labs = labRepository.findAllLabs();
        String uname = userRepository.findUserByUid(uid).getName();
        String lname = lab.getName();
        for (Lab lab1: labs) {
            if(lab.getManage().equals(uname)){
                lname = lab1.getName();
                break;
            }
        }
        try{
            if(lab.getDescription()!=null){
                manageService.updateLabDescriptionByName(uid,lab.getDescription(),lname);
            }
            if(lab.getState() != null){
                manageService.updateStateByName(uid,lab.getState(),lname);
            }
            if(lab.getQuantity() != null){
                manageService.updateQuantityByName(uid,lab.getQuantity(),lname);
            }
            return ResultVO.success("修改成功!");

        }
        catch (Exception e){
            log.error("Error updateMessage", e);
            return ResultVO.error(Code.ERROR, "Failed to updateMessage");
        }
    }

//    @PostMapping("updateUserMessage/{account2}")
//    public ResultVO updateUserMessage(@PathVariable String account2, @RequestBody User user) {
//        try{
//            if(user.getStatus()!=null){
//                adminService.updateStatus(account2, user.getStatus());
//            }
//            if(user.getDescription()!=null){
//                adminService.updateUserDescription(account2, user.getDescription());
//            }
//            if (user.getRole()!=null){
//                adminService.updateRole(account2, user.getRole());
//            }
//            if(user.getPassword()!=null){
//                adminService.updatePassword(account2, user.getPassword());
//            }
//            if(user.getProfession()!=null){
//                adminService.updateProfession(account2, user.getProfession());
//            }
//            return ResultVO.success("修改成功！");
//        }
//        catch(Exception e){
//            log.error("Error updateUserMessage", e);
//            return ResultVO.error(Code.ERROR, "Failed to updateUserMessage");
//        }
//    }


}
